<template>
  <div :class="className" :style="{height:height,width:width}"/>
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import { debounce } from '@/utils'

export default {
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '400px'
    },
    ringData: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      chart: null
    }
  },
  mounted() {
    this.initChart()
    this.__resizeHandler = debounce(() => {
      if (this.chart) {
        this.chart.resize()
      }
    }, 100)
    window.addEventListener('resize', this.__resizeHandler)
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    window.removeEventListener('resize', this.__resizeHandler)
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')

      this.chart.setOption({
        title: {
          text: this.ringData.title,
          x: 'center',
          padding: 16,
          textStyle: {
            color: '#fff'
          }
        },
        tooltip: {
          trigger: 'item',
          formatter: '{b} : {c}kw ({d}%)'
        },
        legend: {
          type: 'scroll',
          left: 'center',
          bottom: this.ringData.legendBottom || 0,
          data: this.ringData.name,
          textStyle: {
            color: '#fff'
          },
          pageIconColor: '#fff',
          pageTextStyle: {
            color: '#23d7cc'
          }
        },

        series: [
          {
            type: 'pie',
            radius: this.ringData.radius || ['40%', '60%'],
            label: {
              formatter: '{b}\n占比：{d}%'
            },
            center: this.ringData.center || ['50%', '55%'],
            data: this.ringData.data,
            animationEasing: 'cubicInOut',
            animationDuration: 2600
          }
        ]
      }, true)
    }
  }
}
</script>

<style>

</style>
